// import { EventSourcePolyfill } from "event-source-polyfill";
import request from '@/utils/request';
export class WebEventSource { 

    source = null;
    constructor(url, options) { 
        try {
            //let token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImxvZ2luX3VzZXJfa2V5IjoiOWU5ZWRiZGUtYmI1NS00NWQwLWEwYmUtNzc2NDA4N2E2NTk1In0.pqgzlqDxtytnUKsC_ShCHTSfCUOkpKUvMBgAIEDJUe6kfnhRLXcrU8a5fTVVsncE3uBo-cPaMRnkrnyLxizDig"
            console.log(' process.env.ENV: ',  process.env.ENV);
            let url =  process.env.NODE_ENV === "production" ? '' : 'http://39.106.226.59:9520';
            this.source = new EventSource(`${url}/prod-api/job/sse/pushMessage/connect/1`,{
                withCredentials: false,
                // headers: {
                //     'Authorization': `Bearer ${token}`,
                //     'Content-Type': 'text/event-stream',
                //     'Cache-Control': 'no-cache',
                //     'Connection': 'keep-alive',
                //     'X-Custom-Header': 'custom-value',
                //     'heartbeatTimeout':60000
                // }
            });
            this.source.onopen  = (e) => { 
                console.log('open', e);
            };
            this.source.onmessage  = (e) => {
                let data = e.data;
                try {
                    data = JSON.parse(data);
                    if(data.type == 1){
                        window.dispatchEvent(new CustomEvent('serverWaringInfo', {detail: data.msg}))
                    }

                } catch (error) {
                    
                }
            };
            this.source.onerror = (e) => { 
                this.source.close();
            };
            
        } catch (error) {
            
        }
    }
}
